﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using System.Web;
using System.Text;
using MS.InformationManagement.DAL.DTOs;
using System.Linq;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class TabsRadicadoProperties : BaseUserControl
    {


        #region Properties

        public bool EnableRemisionUploadControl
        {
            set 
            { 
                dUploadTemplate.Enable = value;
            }
        }

        public bool ShowInboxSelector { set { dUpload.AllowInboxSelection = value; } }

        public bool ShowInboxSelectorOficioRem { set { dUploadTemplate.AllowInboxSelection = value; } }
        public bool ShowLibraryFileOnlyOfRem { get; set; }

        public bool ShowOnlyFilesTab
        {
            set
            {
                liHistory.Visible = !value;
                liSalidas.Visible = !value;

                tab2.Visible = !value;
                tab4.Visible = !value;
            }
        }

        public bool ShowTabSalidas
        {
            set
            {
                liSalidas.Visible = value;
                tab4.Visible = value;
            }
        }
        public bool ShowTabHistorico
        {
            set
            {
                liHistory.Visible = value;
                tab2.Visible = value;
            }
        }

        public bool ShowOficioRemisorioTab
        {
            set
            {
                liOficioRemisorio.Visible = value;
                tab6.Visible = value;
            }
        }

        public bool ShowTabConCopia
        {
            set
            {
                liConCopia.Visible = value;
                tab7.Visible = value;
            }
        }

        public bool ShowDeliveryTabs
        {
            set
            {
                liEnvio.Visible = value;
                tab8.Visible = value;
            }
        }

        public bool Enable
        {
            set
            {
                dUpload.Enable = value;
                dUploadTemplate.Enable = value;
            }
        }

        public List<FileEntity> Files
        {
            get { return dUpload.Files; }
            set
            {
                dUpload.Files = value;
                dUpload.loadData();
            }
        }

        public List<ContactsDTO> Copys
        {
            get { return ConCoipaSelectorID.ContactosList; }
            set
            {
                ConCoipaSelectorID.ContactosList = value;
                ConCoipaSelectorID.BindDataGridResult();
            }
        }

        public IEnumerable<TraceabilityEntry> Logs
        {
            set { ctrLog.DocumentTraceEntries = value; }
        }

        public IEnumerable<DeliveryTraceabilityEntry> DeliveryLogs
        {
            set { DeliveryLogsControl.DeliveryLogsEntries = value == null ? null : value.ToList(); }
        }

        public List<string> ChildRadicados
        {
            set
            {
                child.Childs = value;
            }
        }

        public override event ErrorHandler OnError;

        public SPListItemCollection childRadicados { get; set; }

        public string RadicadoEntrada { get; set; }

        public string url = string.Empty;

        public string strategy
        {
            get
            {
                if (string.IsNullOrEmpty(Page.Request.QueryString["strategy"]))
                    return string.Empty;
                else
                    return Page.Request.QueryString["strategy"];
            }
        }

        #endregion

        #region  Form Actions

        protected void Page_Load(object sender, EventArgs e)
        {
            ConCoipaSelectorID.BindDataGridResult();
            if (!string.IsNullOrEmpty(strategy))
                btnAdd.Visible = false;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            tab5.Visible = false;
            if (ShowLibraryFileOnlyOfRem)
                dUploadTemplate.LibraryFilesOnly = false;
            else
                dUploadTemplate.LibraryFilesOnly = true;
            //dUpload.OnError += new ErrorHandler(dUpload_OnError);
        }

        protected void dUpload_OnError(object sender, Exception e)
        {
            OnError(sender, e);
        }

        public void UploadEnable(bool value)
        {
            dUpload.Enable = value;
        }

        public void DesplegarVentanaModal(string Url, string Titulo, Int32 ancho, Int32 alto, Page pagina)
        {
            StringBuilder scriptModal = new StringBuilder();
            scriptModal.Append("var showMyModalDialog = function() { var options = { ");
            scriptModal.Append("url : '");
            scriptModal.Append(Url);
            //scriptModal.Append("', showMaximized: true,");
            scriptModal.Append("',");
            scriptModal.Append("title: '");
            scriptModal.Append(Titulo);
            scriptModal.Append("', width: ");
            scriptModal.Append(ancho.ToString());
            scriptModal.Append(", height: ");
            scriptModal.Append(alto.ToString());
            scriptModal.Append(", dialogReturnValueCallback: onDialogClose }; SP.UI.ModalDialog.commonModalDialogOpen(" + "'" + Url + "'" + ",options, onDialogClose, null); }; ");
            scriptModal.Append("function onDialogClose(dialogResult, returnValue) { ");
            scriptModal.Append("if (dialogResult == SP.UI.DialogResult.OK) { ");
            scriptModal.Append(" SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } ");
            scriptModal.Append(" } ");

            pagina.ClientScript.RegisterClientScriptBlock(pagina.GetType(), "Mensaje", scriptModal.ToString(), true);
            pagina.ClientScript.RegisterStartupScript(typeof(Page), "Precarga", "ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');", true);
        }

        protected void LnkCC_Click(object sender, EventArgs e)
        {
            DesplegarVentanaModal(SPContext.Current.Web.Url + "/_layouts/MS.InformationManagement.WP/SearchSendersCC.aspx", "Sistema de Gestión Documental", 1000, 460, this.Page);
        }

        #endregion

    }
}
